<template>
  <!--
      	作者：luoyiming
      	时间：2020-06-01
      	描述：插件中心-文章-列表
      -->
  <div>

    <div class="common-seach-wrap">
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="所属栏目">
          <el-select size="small" v-model="searchForm.menu_id" placeholder="所有分类">
            <el-option label="全部" :value="0"></el-option>
            <el-option v-for="(item, index) in menuList" :key="index" :label="item.name" :value="item.menu_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题">
          <el-input size="small" v-model="searchForm.title" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-level-rail">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="openMenu">添加内容</el-button>
    </div>
    <div class="table-wrap">
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="info_id" label="ID" width="60"></el-table-column>
        <el-table-column prop="title" label="标题">
          <template slot-scope="scope">
            <div class="text-ellipsis" :title="scope.row.title">{{scope.row.title}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="image" label="缩略图" width="100">
          <template slot-scope="scope">
            <img v-img-url="'image',scope.row" width="30" height="30" />
          </template>
        </el-table-column>click
        <el-table-column prop="menu.name" label="栏目" width="120"></el-table-column>
        <el-table-column prop="sort" label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
              @change="catSet($event,scope.row.info_id)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="click" label="点击" width="100"></el-table-column>
        <el-table-column prop="sort" label="排序" width="100"></el-table-column>
        <el-table-column prop="create_time" label="添加时间" width="140"></el-table-column>
        <el-table-column prop="update_time" label="更新时间" width="140"></el-table-column>
        <el-table-column prop="name" label="操作" width="90">
          <template slot-scope="scope">
            <el-button @click="editArticle(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="deleteArticle(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
          :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"></el-pagination>
      </div>
      <Menu :is_open="open_menu" @close="closeGetmenuFunc"></Menu>
    </div>
  </div>
</template>

<script>
  import InfoApi from '@/api/info.js';
  import Menu from '@/components/menu/menu.vue';
  export default {
    components: {
      Menu,
    },
    data() {
      return {
        /*分类*/
        categoryData: [],
        /*表单数据*/
        tableData: [],
        /*是否打开添加弹窗*/
        open_add: false,
        /*是否打开编辑弹窗*/
        open_edit: false,
        /*当前编辑的对象*/
        userModel: {},
        commentData: [],
        loading: true,
        /*一页多少条*/
        pageSize: 20,
        /*一共多少条数据*/
        totalDataNumber: 0,
        /*当前是第几页*/
        curPage: 1,
        /*获取用户是否显示*/
        open_menu: false,
        /*搜索参数*/
        searchForm: {
          title: '',
          menu_id: 0
        },
        menuList: [],
      };
    },
    created() {
      /*获取文章列表*/
      this.getTableList();
    },
    methods: {

      /*获取文章列表*/
      getTableList() {
        let self = this;
        let Params = self.searchForm;
        Params.page = self.curPage;
        Params.list_rows = self.pageSize;
        InfoApi.infolist(Params, true)
          .then(data => {
            self.loading = false;
            self.tableData = data.data.list.data;
            self.totalDataNumber = data.data.list.total;
            self.menuList = data.data.menu;
          })
          .catch(error => {
            self.loading = false;
          });
      },
      /*搜索查询*/
      onSubmit() {
        this.curPage = 1;
        this.getTableList();
      },
      /*打开获取栏目*/
      openMenu() {
        this.open_menu = true;
      },
      /*关闭获取栏目*/
      closeGetmenuFunc(e) {
        if (e.type == 'success') {
          this.addMenu(e.params.menu_id, e.params.mould.table);
        }
        this.open_menu = false;
      },
      /*添加内容*/
      addMenu(menu_id, table) {
        if (table == "single") {
          this.$router.push('/info/' + table + '/edit?menu_id=' + menu_id);
        } else {
          this.$router.push('/info/' + table + '/add?menu_id=' + menu_id);
        }

      },

      /*编辑文章*/
      editArticle(row) {
        let info_id = row.info_id;
        let menu_id = row.menu_id;
        let table = row.mould.table;
        this.$router.push({
          path: '/info/' + table + '/edit',
          query: {
            info_id: info_id,
            menu_id: menu_id,
          }
        });
      },
      catSet(e, id) {
        let self = this;
        InfoApi.infoSet({
          info_id: id,
          status: e
        }).then(data => {
          self.$message({
            message: data.msg,
            type: 'success'
          });
        });
      },
      /*选择第几页*/
      handleCurrentChange(val) {
        let self = this;
        self.curPage = val;
        self.loading = true;
        self.getTableList();
      },

      /*每页多少条*/
      handleSizeChange(val) {
        this.curPage = 1;
        this.pageSize = val;
        this.getTableList();
      },

      /*删除文章*/
      deleteArticle(row) {
        let self = this;
        self
          .$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            self.loading = true;
            InfoApi.deleteInfo({
                  info_id: row.info_id
                },
                true
              )
              .then(data => {
                self.$message({
                  message: data.msg,
                  type: 'success'
                });
                self.loading = false;
                self.getTableList();
              })
              .catch(error => {});
          })
          .catch(() => {});
      },
      handleClick(tab, event) {},

      /*打开分类添加*/
      addCategory() {
        this.open_add = true;
      },

      /*打开分类编辑*/
      editCategory(item) {
        this.userModel = item;
        this.open_edit = true;
      },

      /*关闭弹窗*/
      closeDialogFunc(e, f) {
        if (f == 'add') {
          this.open_add = e.openDialog;
          if (e.type == 'success') {
            this.getTableList();
          }
        }
        if (f == 'edit') {
          this.open_edit = e.openDialog;
          if (e.type == 'success') {
            this.getTableList();
          }
        }
      }
    }
  };
</script>

<style></style>
